﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using General.Core.Data;
using General.Entities.Data;
using General.Entities.Models;
using General.Entities.ViewData.Role;
using Microsoft.EntityFrameworkCore;

namespace General.Services.Role
{
    public class RoleService : IRoleService
    {
        private readonly IRepository<tc_role> tc_RoleRepository;

        public RoleService(IRepository<tc_role> tc_roleRepository)
        {
            tc_RoleRepository = tc_roleRepository;
        }


        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleModel>> GetAllList()
        {
            var res = await tc_RoleRepository.GetAllAsync().OrderBy(x=>x.id).ToListAsync();
            var list = new List<RoleModel>();
            foreach (var item in res)
            {
                RoleModel temp = new RoleModel()
                {
                     name = item.rolename, value =item.id
                };
                list.Add(temp);
            }
            return list;
        }

        /// <summary>
        /// 根据id列表获取role列表
        /// </summary>
        /// <param name="uids"></param>
        /// <returns></returns>
        public async Task<List<tc_role>> GetListByIds(string uids)
        {
            var arr = uids.Split(",",StringSplitOptions.RemoveEmptyEntries);
            var arrints = arr.Select<string, int>(x => Convert.ToInt32(x));

            List<tc_role> list = await tc_RoleRepository.GetAllAsync(x => arrints.Contains(x.id)).ToListAsync();
            return list;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="rolename"></param>
        /// <param name="rolecode"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public async Task<AdminResult> GetList(int page, int limit, string rolename, string rolecode, string remark)
        {
            AdminResult result = new AdminResult();
            var query = from d in tc_RoleRepository.Table
                        orderby d.id //descending
                        select new tc_role() {
                             createtime = d.createtime , id=d.id,  remark = d.remark, rolecode=d.rolecode , rolename=d.rolename
                        };
            if (!string.IsNullOrWhiteSpace(rolename))
            {
                query = query.Where(x => x.rolename.Contains(rolename));
            }
            if (!string.IsNullOrWhiteSpace(rolecode))
            {
                query = query.Where(x => x.rolecode.Contains(rolecode));
            }
            if (!string.IsNullOrWhiteSpace(remark))
            {
                query = query.Where(x => x.remark.Contains(remark));
            }
            result.count = await query.CountAsync();
            result.data = await query.Skip((page - 1) * limit).Take(limit).ToListAsync();
            result.code = 0;
            return result;
        }



        /// <summary>
        /// 增加信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task AddInfo(tc_role model)
        {
            model.createtime = DateTime.Now;
            await tc_RoleRepository.insertAsync(model);
        }


        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteInfo(string ids)
        {
            var arr = ids.Split(",", StringSplitOptions.RemoveEmptyEntries).Select<string, int>(x => Convert.ToInt32(x));
            var list = await tc_RoleRepository.GetAllAsync(x => arr.Contains(x.id)).ToListAsync();
            return await tc_RoleRepository.DeleteListAsync(list);
        }

        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task EditInfo(int id, tc_role role)
        {
            var model = await tc_RoleRepository.getByIdAsync(id);
            model.remark = role.remark;
            model.rolecode = role.rolecode;
            model.rolename = role.rolename;
            await tc_RoleRepository.UpdateAsync(model);
        }


    }
}
